package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpServletRequest request){
       ///* ①. 将页面提交的密码password进行md5加密处理, 得到加密后的字符串
        String password = employee.getPassword();
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());

        //②. 根据页面提交的用户名username查询数据库中员工数据信息
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee serviceOne = employeeService.getOne(lqw);

        //③. 如果没有查询到, 则返回登录失败结果
        if (serviceOne==null){
           return R.error("登录失败,用户名或密码错误");
        }

        //④. 密码比对，如果不一致, 则返回登录失败结果
        if (!serviceOne.getPassword().equals(md5Password)){
          return   R.error("登录失败,用户名或密码错误");
        }

        //⑤. 查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (serviceOne.getStatus()==0){
          return   R.error("用户已禁用");
        }

        //⑥. 登录成功，将员工id存入Session, 并返回登录成功结果*/
        request.getSession().setAttribute("employee",serviceOne.getId());

        return R.success(serviceOne);
    }

    //员工退出
    @PostMapping("/logout")
    public R logout(HttpServletRequest request){
        //清理Session中保存的当前登录员工的id
        request.getSession().removeAttribute("employee");
        // B. 返回结果
            return R.success("");
    }

        @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpServletRequest request){
        log.info("新增的员工信息:"+employee);
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);

      /*  employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long employee1 = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(employee1);
        employee.setUpdateUser(employee1);*/

        employeeService.save(employee);
        return R.success("员工新增成功");
    }


@GetMapping("/page")
public R<Page> page(Integer page, Integer pageSize,String name){
        //分页构建器
    Page pageInfo = new Page(page, pageSize);

    //创建条件构建器
    LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();

    //模糊查询
    if (name!=null&&name!=""){
    lqw.like(Employee::getName,name);
    }
    //根据修改时间倒序排序
    lqw.orderByDesc(Employee::getUpdateTime);

    employeeService.page(pageInfo,lqw);

    return R.success(pageInfo);


}
@PutMapping
public R update(@RequestBody Employee employee, HttpServletRequest request){

    Long employee1 = (Long) request.getSession().getAttribute("employee");

    employee.setUpdateUser(employee1);
    employee.setUpdateTime(LocalDateTime.now());
    employeeService.updateById(employee);
    return R.success("修改成功");
}
@GetMapping("/{id}")
public R getById(@PathVariable Long id){

    Employee employee = employeeService.getById(id);
    if (employee!=null){
       return R.success(employee);
    }
    return R.error("未查询到任何信息");
}



}
